A visual tour of Matplotlib.Pyplot, drawing plots, customizing aesthetics, and working with complex charts.
Use ← → keys or swipe to navigate
Importing, basic syntax, and showing plots.
Controlling markers, lines, labels, and grids.
Scatter, Bar, Histogram, and Pie charts.
Combining multiple plots into one figure.
Matplotlib is the most popular data visualization library for Python. It allows you to create static, animated, and interactive visualizations with just a few lines of code.
Most of the Matplotlib utilities lie under the pyplot submodule, acting almost identically to MATLAB's plotting functions.
# Standard import convention import matplotlib.pyplot as plt import numpy as np
The plt.plot() function is used to draw points (markers) in a diagram. By default, it draws a continuous line connecting the points.
xpoints = np.array([0, 6]) ypoints = np.array([0, 250]) # Draw the line plt.plot(xpoints, ypoints) # Display the plot plt.show()
If you only pass Y, Matplotlib assumes X is [0, 1, 2, 3...]
The crucial command that flushes the canvas and displays your final visual on screen.
A "marker" emphasizes specific data points along your line. You specify it using the marker argument in plot().
ypoints = np.array([3, 8, 1, 10]) # 'o' = Circle marker | 'D' = Diamond | '*' = Star plt.plot(ypoints, marker='o', ms=20, mec='r', mfc='y') plt.show()
You can format dynamically using: marker|line|color
Example: plt.plot(ypoints, 'o:r')
(Circle marker, dotted line, red color).
You can change the style, width, and color of the line connecting your data points.
# Creating a thick, dashed, green line plt.plot( ypoints, linestyle='dashed', # or ls='--' color='#06d6a0', # or c='g' linewidth=5.5 # or lw=5.5 )
| Style Name | Shorthand | Description |
|---|---|---|
| 'solid' | '-' | Default connecting line |
| 'dotted' | ':' | Line mapped with dots |
| 'dashed' | '--' | Line separated by dashes |
| 'dashdot' | '-.' | Alternating dashes & dots |
A plot without labels is useless because the viewers don't know what the axes represent.
plt.plot(x, y, label='Sales Data') # Set Title and Axis Labels plt.title('Monthly Revenue', loc='left', fontsize=16) plt.xlabel('Months') plt.ylabel('Revenue (USD)') # Show the legend based on the plot 'label' argument plt.legend()
You can use font dictionaries to style titles dynamically: fontdict = {'family':'serif','color':'blue','size':20} plt.title('Main', fontdict=fontdict)
Grids make your charts dramatically easier to read by adding visual background guides.
# Enable default grid plt.grid() # Enable grid for just one axis plt.grid(axis='x') # Customize grid lines plt.grid(color='green', linestyle='--', linewidth=0.5)
The grid() function takes the same styling parameters as lines, allowing you to establish beautiful, subtle guidelines.
Lines represent trends over time. Next, let's look at specialized plots to represent distributions, comparisons, and comparisons of proportions.
Use scatter() to observe relationships (correlations) between two metric variables. Each item in the array becomes a single dot.
x = np.array([5,7,8,7,2,17,2,9,4]) y = np.array([99,86,87,88,111,86,103,87,94]) plt.scatter(x, y) plt.show()
To make scatter plots 4-Dimensional: Provide an array to c (Colors) and an array to s (Sizes).
plt.scatter(x, y, c=colors, s=sizes, alpha=0.5, cmap='viridis')
Use bar() to compare categorical data (like cities, fruits, or years).
# Vertical Bars x = np.array(["A", "B", "C"]) y = np.array([3, 8, 1]) plt.bar(x, y, width=0.1)
# Horizontal Bars x = np.array(["A", "B", "C"]) y = np.array([3, 8, 1]) plt.barh(x, y, height=0.1)
plt.bar() use width to compress the bars.plt.barh() use height to compress the bars.color parameter.A Histogram shows the frequency distribution of a given 1D array. It bins continuous data to see its spread.
# Generating 250 random values matching a normal distribution # centered at 170, with a std dev of 10 x = np.random.normal(170, 10, 250) plt.hist(x) plt.show()
Bar Charts compare distinct categories.
Histograms measure the distribution of a single continuous numeric variable into discrete 'bins'.
Use pie() to show proportions of a whole.
y = np.array([35, 25, 25, 15]) mylabels = ["Apples", "Bananas", "Cherries", "Dates"] myexplode = [0.2, 0, 0, 0] # pop out the first slice plt.pie(y, labels = mylabels, explode = myexplode, shadow = True) plt.legend(title = "Fruits:") plt.show()
The subplot() function allows you to draw multiple plots in one single figure. It takes a 3-digit integer representing layout: (rows, columns, index)
# Plot 1: Will be drawn as 1st plot in a 1 Row, 2 Column grid plt.subplot(1, 2, 1) plt.plot(x, y) # Plot 2: Will be drawn as 2nd plot in a 1 Row, 2 Column grid plt.subplot(1, 2, 2) plt.plot(x, y) plt.show()
Use plt.suptitle('My Main Dashboard') to add a title reflecting all the subplots combined.
Try these exercises to apply what you've learned. Write these in your Jupyter Notebook.
Given `x=[1, 2, 3, 4]`, `y=[10, 20, 25, 30]`.
Plot a line graph. Make the line dashed, color it red, and add Diamond markers. Add X/Y labels and Title. Finally, display a grid.
Create two arrays: Languages `['Python', 'C++', 'Java']` and Popularity `[90, 50, 75]`.
Generate a Bar Chart. Color Python as 'green', C++ as 'red', and Java as 'blue'.
Create a 2x1 subplot grid (2 rows, 1 col).
Top cell: A Scatter plot showing Revenue vs Marketing Spend.
Bottom cell: A Histogram of Daily Transactions `np.random.normal(5000, 200, 100)`. Add a super title: "Q1 Performance".
It is time to execute these snippets and visualize the data. Complete your experiments in the Lab's interactive environment.